package com.quanxiaoha.xiaohashu.note.biz.rpc;


import com.quanxiaoha.framework.common.response.Response;
import com.quanxiaoha.xiaohashu.kv.api.KeyValueFeignApi;
import com.quanxiaoha.xiaohashu.kv.dto.req.AddNoteContentReqDTO;
import com.quanxiaoha.xiaohashu.kv.dto.req.DeleteNoteContentReqDTO;
import com.quanxiaoha.xiaohashu.kv.dto.req.FindNoteContentReqDTO;
import com.quanxiaoha.xiaohashu.kv.dto.rsp.FindNoteContentRspDTO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * @Author: jingyan_liu
 * @CreateTime: 2025-07-30
 * @Description:
 * @Version: 1.0
 */


@Component
public class KeyValueRpcService {
	
	@Resource
	private KeyValueFeignApi keyValueFeignApi;
	
	/**
	 * 保存笔记内容
	 *
	 * @param uuid
	 * @param content
	 * @return
	 */
	public boolean saveNoteContent(String uuid, String content) {
		AddNoteContentReqDTO addNoteContentReqDTO = new AddNoteContentReqDTO();
		addNoteContentReqDTO.setUuid(uuid);
		addNoteContentReqDTO.setContent(content);
		
		Response<?> response = keyValueFeignApi.addNoteContent(addNoteContentReqDTO);
		
		return !Objects.isNull(response) && response.isSuccess();
	}
	
	/**
	 * 删除笔记内容
	 *
	 * @param uuid
	 * @return
	 */
	public boolean deleteNoteContent(String uuid) {
		DeleteNoteContentReqDTO deleteNoteContentReqDTO = new DeleteNoteContentReqDTO();
		deleteNoteContentReqDTO.setUuid(uuid);
		
		Response<?> response = keyValueFeignApi.deleteNoteContent(deleteNoteContentReqDTO);
		
		return !Objects.isNull(response) && response.isSuccess();
	}
	
	public String findNoteContent(String uuid) {
		FindNoteContentReqDTO findNoteContentReqDTO = new FindNoteContentReqDTO();
		findNoteContentReqDTO.setUuid(uuid);
		
		Response<FindNoteContentRspDTO> response = keyValueFeignApi.findNoteContent(findNoteContentReqDTO);
		
		if (Objects.isNull(response) || !response.isSuccess() || Objects.isNull(response.getData())) {
			return null;
		}
		
		return response.getData().getContent();
	}
	
}
